/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package swa.e_store.businessLogic;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import swa.e_store.DTO.ArticleDTO;
import swa.e_store.DTO.OfferDTO;
import swa.e_store.entity.Article;
import swa.e_store.entity.Offer;


/**
 *
 * @author Stefan
 */
public class BL_Article {
    
    public BL_Article(){
        
    }
    
    public ArticleDTO createArticleTable(){
        String[] columnNames = {"ArtNbr", "Description", "Price", "OfferID"};
        
        Object[][] data;
        
        //GetArticles
        List<Article> artList = getAllArticles();
        
        
        // Fill Data.
        Iterator<Article> it = artList.iterator();
        data = new Object[artList.size()][4];
        int i=0;
        while(it.hasNext()) {
            Article article = it.next();
            data[i][0]= article.getArtNbr();
            data[i][1]= article.getDescription();
            data[i][2]= article.getPrice();
            data[i][3]= article.getOfferId();
            i++;
        }
        
        // In DTO Abfüllen
        
        ArticleDTO artDTO = new ArticleDTO();
        artDTO.setColumnNames(columnNames);
        artDTO.setData(data);
        
        return artDTO;
    }
    
     /**
     *  Returns a list of all articles.
     * 
     * @return 
     */
        public List<Article> getAllArticles(){
            
        //EMFactory
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Napier_E-StorePU");
        
        //EntityManager
        EntityManager eManager = emFactory.createEntityManager();
        
        //Start Transaction
        EntityTransaction tx = eManager.getTransaction();
        tx.begin();
        
        try
        {
            List<Article> articles = eManager.createNamedQuery("article.findAll").getResultList();
            return articles;
        }
        catch (Exception ex)
        {
            System.out.println(ex);
            return null;
        }
        finally
        {
            eManager.close();
            emFactory.close();
        }
        
    }
  
        
    public Article setPrice(int artNbr, float price){
        
                //EMFactory
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Napier_E-StorePU");
        
        //EntityManager
        EntityManager eManager = emFactory.createEntityManager();
        
        //Start Transaction
        EntityTransaction tx = eManager.getTransaction();
        tx.begin();
       
        
        Article article = eManager.find(Article.class, artNbr);
        article.setPrice(price);
        
        tx.commit();
        
        //Prüfen
        Article checkedArticle = eManager.find(Article.class, artNbr);
        System.out.println("Prüfung ob Arikel mit Preis geändert: " + checkedArticle);
        
        //dispose
        eManager.close();
        emFactory.close();
        
        return checkedArticle;

    }

    public void setOffer(int artNbr, int offerId){
        //EMFactory
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Napier_E-StorePU");
        
        //EntityManager
        EntityManager eManager = emFactory.createEntityManager();
        
        //Start Transaction
        EntityTransaction tx = eManager.getTransaction();
        tx.begin();
       
        
        Article article = eManager.find(Article.class, artNbr);
        article.setOfferId(offerId);
        
        tx.commit();
        
        //Prüfen
        Article checkedArticle = eManager.find(Article.class, artNbr);
        System.out.println("Prüfung ob Arikel mit Offer geändert: " + checkedArticle);
        
        //dispose
        eManager.close();
        emFactory.close();
    }
    
//// :TODO Mit nem SQL-Abfragestring realisieren.
//    
//    /**
//     * Select articles by offers they are on.
//     * 
//     * @param offerId
//     * @return a collection of the selected Articles.
//     */
//    public ArrayList<Article> selectByOffers(int offerId){
//        
//    }
    

    
    
    /**
     * Creates a new offer-Type.
     * 
     * @param id
     * @param description
     * @return 
     */
    public Offer createOffer(int id, String description){
        //EMFactory
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Napier_E-StorePU");
        
        //EntityManager
        EntityManager eManager = emFactory.createEntityManager();
        
        //Start Transaction
        EntityTransaction tx = eManager.getTransaction();
        tx.begin();
       
        
        Offer offer = new Offer(id, description);
        eManager.persist(offer);
        tx.commit();
        
        //dispose
        eManager.close();
        emFactory.close();
        
        return offer;
    }

    OfferDTO createOfferTable() {
        String[] columnNames = {"OfferId", "Description"};
        
        Object[][] data;
        
        //GetArticles
        List<Offer> offerList = getAllOffers();
        
        
        // Fill Data.
        Iterator<Offer> it = offerList.iterator();
        data = new Object[offerList.size()][4];
        int i=0;
        while(it.hasNext()) {
            Offer offer = it.next();
            data[i][0]= offer.getId();
            data[i][1]= offer.getDescription();

            i++;
        }
        
        // In DTO Abfüllen
        
        OfferDTO offerDTO = new OfferDTO();
        offerDTO.setColumnNames(columnNames);
        offerDTO.setData(data);
        
        return offerDTO;
    }
    
        public List<Offer> getAllOffers(){
            
        //EMFactory
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Napier_E-StorePU");
        
        //EntityManager
        EntityManager eManager = emFactory.createEntityManager();
        
        //Start Transaction
        EntityTransaction tx = eManager.getTransaction();
        tx.begin();
        
        try
        {
            List<Offer> offers = eManager.createNamedQuery("offer.findAll").getResultList();
            return offers;
        }
        catch (Exception ex)
        {
            System.out.println(ex);
            return null;
        }
        finally
        {
            eManager.close();
            emFactory.close();
        }
        
    }

    ArticleDTO createArticleTableByOffer(int offerId) {
        String[] columnNames = {"ArtNbr", "Description", "Price", "OfferID"};
        
        Object[][] data;
        
        //GetArticles
        List<Article> artList = getArticlesByOffer(offerId);
        
        
        // Fill Data.
        Iterator<Article> it = artList.iterator();
        data = new Object[artList.size()][4];
        int i=0;
        while(it.hasNext()) {
            Article article = it.next();
            data[i][0]= article.getArtNbr();
            data[i][1]= article.getDescription();
            data[i][2]= article.getPrice();
            data[i][3]= article.getOfferId();
            i++;
        }
        
        // In DTO Abfüllen
        
        ArticleDTO artDTO = new ArticleDTO();
        artDTO.setColumnNames(columnNames);
        artDTO.setData(data);
        
        return artDTO;
    }

    private List<Article> getArticlesByOffer(int offerId) {
            
        //EMFactory
        EntityManagerFactory emFactory = Persistence.createEntityManagerFactory("Napier_E-StorePU");
        
        //EntityManager
        EntityManager eManager = emFactory.createEntityManager();
        
        //Start Transaction
        EntityTransaction tx = eManager.getTransaction();
        tx.begin();
        
        try
        {
            List<Article> articles = eManager.createNamedQuery("article.findByOffer").setParameter("offerId", offerId).getResultList();
            return articles;
        }
        catch (Exception ex)
        {
            System.out.println(ex);
            return null;
        }
        finally
        {
            eManager.close();
            emFactory.close();
        }
        
    }
}
